package com.mjh.zhujianju.service.supervise.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.utils.DeleteType;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.supervise.SupervisionDiscussMapper;
import com.mjh.zhujianju.enums.SupervisionDiscussIsAll;
import com.mjh.zhujianju.model.supervise.SupervisionDiscussEO;
import com.mjh.zhujianju.model.supervise.SupervisionUsersEO;
import com.mjh.zhujianju.service.supervise.SupervisionDiscussService;
import com.mjh.zhujianju.service.supervise.SupervisionService;
import com.mjh.zhujianju.service.supervise.SupervisionUsersService;
import com.mjh.zhujianju.util.FileUrlAdapter;
import com.mjh.zhujianju.vo.supervision.SupervisionVO;

@Service
public class SupervisionDiscussServiceImpl extends BaseServiceImpl<SupervisionDiscussEO> implements SupervisionDiscussService{

	@Resource
	private SupervisionDiscussMapper supervisionDiscussMapper;
	
	@Resource
	private SupervisionUsersService supervisionUsersService;
	
	@Resource
	private SysUserService userService;

	@Resource
	private SupervisionService supervisionService;
	
	public SupervisionDiscussServiceImpl(SupervisionDiscussMapper supervisionDiscussMapper) {
		super(supervisionDiscussMapper);
	}

	@Override
	@Transactional
	public void saveInfo(SupervisionDiscussEO eo) throws XTBusinessException {
		eo.setStatus(EStatus.active.getCode());
		eo.setUserId(XTCurrentUser.getCurrentUserId());
		if(eo.getToUserId()==null) {//@所有人
			eo.setIsAll(SupervisionDiscussIsAll.YES.getCode());
			this.saveSelective(eo);
			List<SupervisionUsersEO>  users =	supervisionUsersService.getUserListBysupervisionId(eo.getPermissionId());
			if(!XTListUtils.isEmpty(users)) {
				Long id = eo.getId();
				for (SupervisionUsersEO supervisionUsersEO : users) {
					if(supervisionUsersEO.getUserId().equals(eo.getUserId())) {
						continue;
					}
					eo.setIsAll(SupervisionDiscussIsAll.YES.getCode());
					eo.setToUserId(supervisionUsersEO.getUserId());
					eo.setParentId(id);
					eo.setId(null);
					this.saveSelective(eo);
				}
			}
		}else {
			eo.setIsAll(SupervisionDiscussIsAll.NO.getCode());
			this.saveSelective(eo);
		}
	}

	@Override
	@Transactional
	public void updateInfo(SupervisionDiscussEO eo) throws XTBusinessException {
		eo.setStatus(EStatus.active.getCode());
		this.updateByPrimaryKeySelective(eo);
	}

	@Override
	public PagedResult<SupervisionDiscussEO> getVOPage(SupervisionDiscussEO vo) {
		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		vo.setUserId(XTCurrentUser.getCurrentUserId());
		List<SupervisionDiscussEO> list = supervisionDiscussMapper.selectPagination(vo);
		if(!XTListUtils.isEmpty(list)) {
			List<Long> userIds = new ArrayList<>();
			for (SupervisionDiscussEO SupervisionDiscussEO : list) {
				userIds.add(SupervisionDiscussEO.getUserId());
				userIds.add(SupervisionDiscussEO.getToUserId());
			}
			Map<Long, SysUser>  userMap =	userService.getMapByIdList(userIds);
			if(userMap!=null) {
				for (SupervisionDiscussEO supervisionDiscussEO : list) {
					SysUser user = userMap.get(supervisionDiscussEO.getUserId());
					if(user!=null) {
						supervisionDiscussEO.setNickname(user.getNickname());
					}
					SysUser toUser = userMap.get(supervisionDiscussEO.getToUserId());
					if(toUser!=null) {
						supervisionDiscussEO.setToNickname(toUser.getNickname());	
					}
				}	
			}
		}
		PagedResult<SupervisionDiscussEO> page = PagedBeanUtil.toPagedResult(list);
		return page;
	}

	@Override
	public SupervisionDiscussEO getInfo(Long id) throws XTBusinessException {
		SupervisionDiscussEO wd = super.getByPrimaryKey(id);
		return wd;
	}

	@Override
	public void del(Long id) throws XTBusinessException {
		SupervisionDiscussEO eo = this.supervisionDiscussMapper.selectByPrimaryKey(id);
		if(eo.getToUserId().equals(XTCurrentUser.getCurrentUserId())) {//接收消息对象删除
			eo.setToIsDel(DeleteType.IS_DELETE);
			this.updateByPrimaryKeySelective(eo);
		}else {
			this.delByPrimaryKey(eo);
		}
	}

	@Override
	public SupervisionVO findSupervisionDiscussById(Long id) throws XTBusinessException {
		SupervisionVO supervisionVO = new SupervisionVO();
		Map<Long,Long> userMap = new HashMap<Long, Long>();
		List<SupervisionUsersEO> supervisionUsersEOS = supervisionUsersService.getUserListBysupervisionId(id);
		List<Long> ids = supervisionUsersEOS.stream().map(supervisionUsersEO -> supervisionUsersEO.getUserId()).distinct().collect(Collectors.toList());
		//查询该督办有哪些人员
		Map<Long, SysUser> sysUserMap = userService.getMapTopOne(ids);
		if(!CollectionUtils.isEmpty(supervisionUsersEOS)){
			for(SupervisionUsersEO supervisionUsersEO : supervisionUsersEOS){
				userMap.put(supervisionUsersEO.getUserId(), supervisionUsersEO.getHeadImage());
				SysUser sysUser = sysUserMap.get(supervisionUsersEO.getUserId());
				if(sysUser != null){
					supervisionUsersEO.setRemark2(FileUrlAdapter.assemblyAction(Long.valueOf(supervisionUsersEO.getHeadImage())));
					supervisionUsersEO.setNickname(sysUser.getNickname());
				}

			}
		}
		supervisionVO.setSupervisionUserList(supervisionUsersEOS);
		//查询交流信息
		SupervisionDiscussEO temp = new SupervisionDiscussEO();
		temp.setPermissionId(id);
		List<SupervisionDiscussEO> supervisionDiscussEOS = supervisionDiscussMapper.selectListByCondition(temp);
		if(!CollectionUtils.isEmpty(supervisionDiscussEOS)){
			for(SupervisionDiscussEO supervisionDiscussEO : supervisionDiscussEOS){
				SysUser sysUser = sysUserMap.get(supervisionDiscussEO.getUserId());
				if(sysUser != null){
					supervisionDiscussEO.setNickname(sysUser.getNickname());
					if(userMap!=null) {
						supervisionDiscussEO.setRemark2(FileUrlAdapter.assemblyAction(userMap.get(supervisionDiscussEO.getUserId())));
					}
				}

				SysUser sysToUser = sysUserMap.get(supervisionDiscussEO.getToUserId());
				if(sysToUser != null){
					supervisionDiscussEO.setToNickname(sysToUser.getNickname());
					if(userMap!=null) {
						supervisionDiscussEO.setRemark2(FileUrlAdapter.assemblyAction(userMap.get(supervisionDiscussEO.getToUserId())));
					}
				}
				
			}
		}
		supervisionVO.setSupervisionDiscussEOS(supervisionDiscussEOS);


		return supervisionVO;
	}

}
